package com.imooc.bilibili.controller;

import com.alibaba.fastjson.JSONObject;
import com.imooc.bilibili.entity.LoginInfo;
import com.imooc.bilibili.entity.UserInfo;
import com.imooc.bilibili.entity.page.PageResult;
import com.imooc.bilibili.request.UserRequest;
import com.imooc.bilibili.result.Result;
import com.imooc.bilibili.service.UserFollowService;
import com.imooc.bilibili.service.UserService;
import com.imooc.bilibili.support.UserSupport;
import com.imooc.bilibili.utils.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user_service")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserSupport userSupport;

    @Autowired
    private UserFollowService userFollowService;

    @GetMapping("/get-users")
    public Result<UserInfo> getLoginInfoService() {
        //获取用户的登录信息id
        Map<String, Long> currnetMap = userSupport.getCurrnetUserId();
        Long currnetUserId = currnetMap.get("loginId");
        UserInfo userInfo = userService.getUserInfoService(currnetUserId);
        return new Result<>(userInfo);
    }

    /**
     * 获取RSA公钥
     */
    @GetMapping("/rsa-pks")
    public Result<String> getRsaPublicKey() {
        Map<String, String> publicKeyMap = RsaUtils.generateKey();
        String pk = publicKeyMap.get("publicKeyStr");
        return new Result<>(pk);
    }

    /**
     * 用户注册接口
     */
    @PostMapping("/register-users")
    public Result<String> insertUser(@RequestBody UserRequest req) {
        userService.insertUserService(req);
        return Result.success();
    }

    /**
     * 用户登录接口
     * 如果登录成功返回前端token
     */
    @PostMapping("/login-users-tokens")
    public Result<String> loginUser(@RequestBody LoginInfo loginInfo) {
        String loginToken = userService.loginUserService(loginInfo);
        return new Result<>(loginToken);
    }

    @PutMapping("/user-infos-update")
    public Result<String> updateUserInfo(@RequestBody UserRequest request) {
        //获取用户id
        Map<String, Long> currnetMap = userSupport.getCurrnetUserId();
        Long userId = currnetMap.get("userId");
        request.setUserId(userId);
        userService.updateUserInfoService(request);
        return Result.success();
    }

    @GetMapping("/user-infos")
    public Result<PageResult<UserInfo>> getUserInfoPageList(@RequestParam("number") Integer number,
                                                                  @RequestParam("size") Integer size,
                                                                  String nick) {
        Map<String, Long> currnetUserId = userSupport.getCurrnetUserId();
        Long userId = currnetUserId.get("userId");
        JSONObject params = new JSONObject();
        params.put("number",number);
        params.put("size",size);
        params.put("nick",nick);
        params.put("userId",userId);
        PageResult<UserInfo> userInfoPageService = userService.getUserInfoPageService(params);
        if (userInfoPageService.getTotal() > 0) {
           List<UserInfo> checkedUserInfo = userFollowService.checkFollowingStatus(userInfoPageService.getPageInfo(),userId);
           userInfoPageService.setPageInfo(checkedUserInfo);
        }
        return new Result<>(userInfoPageService);
    }


    /**
     * 登录接口更新，返回双token：登录token和刷新token
     * @param loginInfo
     * @return
     */
    @PostMapping("/login-user-dts")
    public Result<Map<String,String>> loginUserDts(@RequestBody LoginInfo loginInfo) {
        Map<String,String> map = userService.loginUserDtsService(loginInfo);
        return new Result<>(map);
    }

    /**
     * 退出登录时，删除refreshToken
     * @param request
     * @return
     */
    @DeleteMapping("/refresh-tokens")
    public Result<String> logOut(HttpServletRequest request) {
        String refreshToken = request.getHeader("refreshToken");

        Long userId = userSupport.getCurrnetUserId().get("userId");
        userService.logoutService(refreshToken,userId);
        return Result.success();
    }

    /**
     * 使用refreshToken刷新accessToken
     * @return
     */
    @PostMapping("/access-tokens")
    public Result<String> refreshAccessToken(HttpServletRequest request) {
        Long userId = userSupport.getCurrnetUserId().get("userId");

        String refreshToken = request.getHeader("refreshToken");
        String accessToken = userService.refreshAccessToken(refreshToken);
        return new Result<>(accessToken);
    }
}
